package com.sgb.kjwl.view.widget.manage

import android.app.Activity
import android.app.Dialog
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.view.*
import androidx.databinding.DataBindingUtil
import androidx.fragment.app.DialogFragment
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.sgb.kjwl.R
import com.sgb.kjwl.databinding.FragmentChooseTypeBinding
import com.sgb.kjwl.databinding.ItemChooseTypeBinding
import com.sgb.link.entity.PickFilterEntity
import com.swgk.core.recyclerview.decoration.RecyclerCustomDecoration
import java.io.Serializable

/**
 * 询价或者邀请选择
 */
class ChooseTypeFragment : DialogFragment() {

    private var mDatas : List<PickFilterEntity<Any>> ?= null

    private lateinit var mBinding : FragmentChooseTypeBinding

    private var mCallBack : ClickItemCallBack ?= null

    private lateinit var mAdapter : TypeAdapter

    override fun onAttach(activity: Activity) {
        super.onAttach(activity)
        if (activity  is ClickItemCallBack){
            mCallBack = activity
        }
    }

    companion object{
        const val TAG = "chooseTypeFragment"
        const val KEY_DATAS = "key_datas"
        fun newInstance(datas : List<PickFilterEntity<*>>) : ChooseTypeFragment{
            val args = Bundle()
            val fragment = ChooseTypeFragment()
            args.putSerializable(KEY_DATAS, datas as Serializable)
            fragment.arguments = args
            return fragment
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        if (arguments != null){
            mDatas = requireArguments().get(KEY_DATAS) as List<PickFilterEntity<Any>>?
        }
        isCancelable = false
    }


    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        mBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_choose_type, container, false)
        mBinding.recycleView.setItemViewCacheSize(5)
        mBinding.recycleView.layoutManager = LinearLayoutManager(context, RecyclerView.VERTICAL, false)
        mAdapter = TypeAdapter(R.layout.item_choose_type)
        mAdapter.bindToRecyclerView(mBinding.recycleView)
        mBinding.recycleView.addItemDecoration(
            RecyclerCustomDecoration(
                0f,
                0f,
                0f,
                resources.getDimension(R.dimen.dp1), resources.getColor(R.color.color_line)
            )
        )
        mAdapter.setNewData(mDatas)
        mAdapter.setOnItemClickListener { adapter, view, position ->
            if (mCallBack != null) {
                mCallBack!!.clickItem(mAdapter.getItem(position)!!)
            }
            dismiss()
        }
        mBinding.cancel.setOnClickListener {
            dismiss()
        }
        return mBinding.root
    }

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        val dialog = super.onCreateDialog(savedInstanceState)
        val win = dialog.window
        win.decorView.setPadding(0, 0, 0, 0)
        val lp = win.attributes
        win.setBackgroundDrawable(ColorDrawable(resources.getColor(R.color.transparent)))
        lp.gravity = Gravity.BOTTOM
        lp.windowAnimations = R.style.popwindow_down_up
        lp.width = WindowManager.LayoutParams.MATCH_PARENT
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT
        win.attributes = lp
        return dialog
    }

    interface ClickItemCallBack{
        fun clickItem(entity : PickFilterEntity<Any>)
    }



    class TypeAdapter(layoutResId : Int) : BaseQuickAdapter<PickFilterEntity<Any>, BaseViewHolder>(layoutResId){
        private var mItemBinding : ItemChooseTypeBinding ?= null
        override fun convert(helper: BaseViewHolder?, item: PickFilterEntity<Any>?) {
           mItemBinding = DataBindingUtil.bind(helper!!.itemView)
            if (mItemBinding != null){
                mItemBinding!!.tvName.text = item!!.name
            }
        }

    }

}